<?php

class PagesModel
{
    public function AddPage( $page )
    {
        require_once "../" . Settings::DBPath();
        $DB = new Database();

        if ( $DB->Connect() )
        {
            $stmt = $DB->Prepare( "INSERT INTO pages ( Title, Published ) VALUES ( ?, ? )" );
            $stmt->bind_param( "si", $page['title'], $page['published'] );
            $stmt->execute();
            $stmt->close();
            
            $stmt = $DB->Prepare( "INSERT INTO sections ( sections.Order, Width, PageID ) VALUES ( 1, '100%', LAST_INSERT_ID() )" );
            $stmt->execute();
            $stmt->close();
            
            $stmt = $DB->Prepare( "INSERT INTO drafts ( Date, Content, EditedBy, SectionID ) VALUES ( now(), 'New Section', '1', LAST_INSERT_ID() )" );
            $stmt->execute();
            $stmt->close();
            
            $DB->Close();

            return true;
        }

        return false;
    }
    
    public function DeletePage( $pageID )
    {
        require_once "../" . Settings::DBPath();
        $DB = new Database();

        if ( $DB->Connect() )
        {
            $stmt = $DB->Prepare( "DELETE FROM Pages WHERE PageID = ?" );
            $stmt->bind_param( "i", $pageID );
            $stmt->execute();
            $stmt->close();
            
            $DB->Close();
            
            return true;
        }

        return false;
    }
    
    public function UpdatePage( $page )
    {
        require_once "../" . Settings::DBPath();
        $DB = new Database();

        if ( $DB->Connect() )
        {
            $stmt = $DB->Prepare( "UPDATE Pages SET Title = ?, Published = ? WHERE PageID = ?" );
            $stmt->bind_param( "sii", $page['title'], $page['published'], $page['id'] );
            $stmt->execute();
            $stmt->close();
            
            $DB->Close();
            
            return true;
        }
		
        return false;
    }
	
	public function GetPages()
	{
		require_once "../" . Settings::DBPath();
        $DB = new Database();

		$pages = array();
		
        if ( $DB->Connect() )
        {
            $stmt = $DB->Prepare( "SELECT PageID, Title, Published FROM pages" );
            $stmt->execute();
			$stmt->bind_result( $row_pageId, $row_title, $row_published );
			
			while ( $stmt->fetch() )
			{
				$pages[] = array(   'id' => $row_pageId,
									'title' => $row_title,
									'published' => $row_published );
			}
			
            $stmt->close();
            
            $DB->Close();
        }

        return $pages;
	}
	
	public function GetPage( $pageID )
	{
		require_once "../" . Settings::DBPath();
        $DB = new Database();

		$page = null;
		
        if ( $DB->Connect() )
        {
            $stmt = $DB->Prepare( "SELECT PageID, Title, Published FROM pages WHERE PageID=?" );
			$stmt->bind_param( "i", $pageID );
            $stmt->execute();
			$stmt->bind_result( $row_pageId, $row_title, $row_published );
			$stmt->fetch();
			
			$page = array(   'id' => $row_pageId,
									'title' => $row_title,
									'published' => $row_published );
			
            $stmt->close();
            
            $DB->Close();
        }

        return $page;
	}
}
